﻿/******************************************************************************************
 * 
 * Copyright (c) 2012 WU WAI FAN DENNIS
 * 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
******************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Interaction.Compiler
{
    public class GotoActivity : AbstractActivity, IActivity
    {
        public GotoActivity() : base()
        {

        }

        public bool compile()
        {
            base.compile();

            bool ret = false;
            Node valueNode = FindFirstNode();
            Node nodeEntry = Compiler.mapLink[valueNode];
            IActivity farActivity = FindActivityByEntry(nodeEntry);
            mapNodeValue["[%DONE%]"] = farActivity;
            Page thisPage = ThisPage();

            strCurrentExt = FindExtension(this);
            if (strCurrentExt.Trim().Length <= 0)
            {
                Compiler.PrintError("Disconnected link", "");
                return false;
            }


            if (Compiler.mapPage.ContainsKey(farActivity.DocID))
            {
                Page page = Compiler.mapPage[farActivity.DocID];

                string strVal = "";
                string strGotoLabel = "";

                if (attribute.ContainsKey("extension"))
                {
                    strVal = attribute["extension"];
                    if (strVal.Trim().Length <= 0)
                    {
                        Compiler.PrintError("Activity '" + strDesc + "' in '" + this.FindFlow(strDocID) + "' - variable " + strVal + " not be defined.", "");
                        return ret;
                    }
                    else if (VariableExist(strVal, true))
                    {
                        strVal = "${" + strVal + "}";
                    }
                }
                if (attribute.ContainsKey("gotolabel"))
                {
                    strGotoLabel = attribute["gotolabel"];
                    if (strGotoLabel.Trim().Length <= 0)
                    {
                        Compiler.PrintError("Label '" + strDesc + "' in '" + this.FindFlow(strDocID) + "' - variable " + strVal + " not be defined.", "");
                        return ret;
                    }
                }


                //Goto label
                lpInstruction = new ArrayList();

                InstructionSet set = new InstructionSet();
                set.ExtNumber = strCurrentExt;
                set.Label = strDesc.Replace(' ', '_');
                if (thisPage.DocID == page.DocID)
                {
                    if (strGotoLabel.ToLower() == "start")
                    {
                        set.Instruction = string.Format("Goto({0},{1})", new object[] { strVal, 1 });
                    }
                    else
                    {
                        set.Instruction = string.Format("Goto({0},{1})", new object[] { strVal, strGotoLabel });
                    }
                }
                else
                {
                    if (strGotoLabel.ToLower() == "start")
                    {
                        set.Instruction = string.Format("Goto({0},{1},{2})", new object[] { page.FlowName.Replace(' ', '_'), strVal, 1 });
                    }
                    else
                    {
                        set.Instruction = string.Format("Goto({0},{1},{2})", new object[] { page.FlowName.Replace(' ', '_'), strVal, strGotoLabel });
                    }
                }
                lpInstruction.Add(set);

                priorityOffset = lpInstruction.Count - 1;

                ret = true;
            }
            else
            {
                Compiler.PrintError("Subflow for " + strDesc + " not be defined.", "");
            }

            return ret;
        }
    }
}
